Page({
  data: {
    wrongWordsList: [
      { id: 1, word: 'abandon', pronunciation: '/əˈbændən/', definition: '放弃，抛弃', example: 'She abandoned her plans to become an actress.', wrongCount: 3 },
      { id: 2, word: 'accommodate', pronunciation: '/əˈkɒmədeɪt/', definition: '容纳，提供住宿', example: 'The hotel can accommodate up to 500 guests.', wrongCount: 2 },
      { id: 3, word: 'ambiguous', pronunciation: '/æmˈbɪɡjuəs/', definition: '模棱两可的，含糊的', example: 'His instructions were ambiguous.', wrongCount: 4 },
      { id: 4, word: 'assess', pronunciation: '/əˈses/', definition: '评估，评价', example: 'The teacher assessed the students\' performance.', wrongCount: 1 },
      { id: 5, word: 'authorize', pronunciation: '/ˈɔːθəraɪz/', definition: '授权，批准', example: 'The manager authorized the payment.', wrongCount: 2 }
    ],
    currentPage: 1,
    pageSize: 10,
    totalCount: 5
  },

  // 查看单词详情
  viewWordDetail(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/wordDetail/wordDetail?id=${id}&from=wrongWords`
    })
  },

  // 移除常错单词
  removeWrongWord(e) {
    const { id } = e.currentTarget.dataset
    wx.showModal({
      title: '提示',
      content: '确定要移除这个常错单词吗？',
      success: (res) => {
        if (res.confirm) {
          const { wrongWordsList } = this.data
          const newList = wrongWordsList.filter(item => item.id !== id)
          this.setData({
            wrongWordsList: newList,
            totalCount: newList.length
          })
          wx.showToast({
            title: '移除成功',
            icon: 'success'
          })
        }
      }
    })
  },

  // 清除所有常错单词
  clearAllWrongWords() {
    wx.showModal({
      title: '提示',
      content: '确定要清除所有常错单词吗？',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            wrongWordsList: [],
            totalCount: 0
          })
          wx.showToast({
            title: '清除成功',
            icon: 'success'
          })
        }
      }
    })
  },

  // 分页变化
  onPageChange(e) {
    this.setData({
      currentPage: e.detail.current
    })
  },

  onLoad: function () {
    // 模拟从服务器加载常错单词数据
    this.loadWrongWords()
  },

  // 加载常错单词数据
  loadWrongWords() {
    // 这里是模拟数据，实际开发中应该从服务器获取
    const { wrongWordsList } = this.data
    this.setData({
      wrongWordsList,
      totalCount: wrongWordsList.length
    })
  }
})